package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.HeartKnowledge;
import com.czh.springboot04.service.HeartKnowledgeService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/heartKnowledge")
public class HeartKnowledgeController {
    @Autowired
    private HeartKnowledgeService heartKnowledgeService;
    @PostMapping("add")
    public R add(@RequestBody HeartKnowledge heartKnowledge){
        int i=heartKnowledgeService.addHeartKnowledge(heartKnowledge);
        if(i> 0 ){
            return R.ok(heartKnowledge);
        }else {
            return R.fail("添加失败");
        }
    }
    @PutMapping("update")
    public R update(@RequestBody HeartKnowledge heartKnowledge){
        int i=heartKnowledgeService.updateHeartKnowledge(heartKnowledge);
        if(i> 0 ){
            return R.ok("修改成功");
        }else {
            return R.fail("失败");
        }
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable int id){
        int i=heartKnowledgeService.deleteHeartKnowledge(id);
        if(i> 0 ){
            return R.ok("删除成功");
        }else {
            return R.fail("失败");
        }
    }
    @GetMapping("findAll")
    public R findAll(@RequestParam(required = false,defaultValue = "1") int pageNum,
                     @RequestParam(required = false,defaultValue = "3") int pageSize){
        PageInfo<HeartKnowledge> pageInfo=heartKnowledgeService.findAll(pageNum,pageSize);
        return R.ok(pageInfo);
    }
    @GetMapping("findByName")
    public R findByName(@RequestParam(required = false,defaultValue = "1") int pageNum,
                     @RequestParam(required = false,defaultValue = "3") int pageSize,
                        @RequestParam(required = false,defaultValue = "") String name){
        PageInfo<HeartKnowledge> pageInfo=heartKnowledgeService.findByName(pageNum,pageSize,name);
        if (pageInfo != null) {
            return R.ok(pageInfo);
        } else {
            return R.fail("查询失败");
        }
    }
}
